<?php

namespace app\models;

use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class Auser extends ActiveRecord implements IdentityInterface
{
    public static function tableName()
    {
        return '{{%admin}}';
    }

    /**
     * 根据给的id查询用户身份
     *
     * @param int|string $id the id of person
     *
     * @return null|static
     */
    public static function findIdentity($id)
    {
        // TODO: Implement findIdentity() method.
        return static::findOne($id);
    }

    /**
     * 根据token查询身份
     *
     * @param mixed $token The token of the user
     * @param null  $type  type
     *
     * @return null|static
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        // TODO: Implement findIdentityByAccessToken() method.
        return static::findOne(['access_token' => $token]);
    }

    /**
     * Get user id
     *
     * @return mixed
     */
    public function getId()
    {
        // TODO: Implement getId() method.
        return $this->id;
    }

    /**
     * Return cookie key
     *
     * @return mixed
     */
    public function getAuthKey()
    {
        // TODO: Implement getAuthKey() method.
//        return $this->auth_key;
    }

    /**
     * Validate AuthKey
     *
     * @param string $authKey authKey
     *
     * @return bool
     */
    public function validateAuthKey($authKey)
    {
        // TODO: Implement validateAuthKey() method.
        return $this->getAuthKey() === $authKey;
    }


    public static function findByUsername($username)
    {
        $user = Auser::find()->where(['username' => $username])->one();
        return new static($user);
    }


}